<?php

/**
 * This is the model class for table "investor_admin".
 *
 * The followings are the available columns in table 'investor_admin':
 * @property string $admin_id
 * @property string $admin_name
 * @property string $username
 * @property string $email
 * @property string $password
 * @property integer $block
 * @property integer $send_email
 * @property string $register_date
 * @property string $lastvisit_date
 * @property string $activation
 */
class User extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return User the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return '{{admin}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('username, email, password', 'required'),
            // convert username to lower case
            array('username', 'filter', 'filter'=>'strtolower'),
            array('password','length','max'=>64, 'min'=>6),
            // make sure email is a valid email
            array('email','email'),
            // make sure username and email are unique
            array('username, email', 'unique'),
            array('block, send_email', 'numerical', 'integerOnly' => true),
            array('admin_name, username, email, activation', 'length', 'max' => 100),
            array('register_date, lastvisit_date', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('admin_id, admin_name, username, email, password, block, send_email, register_date, lastvisit_date, activation', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'admin_id' => 'Id',
            'admin_name' => 'Full Name',
            'username' => 'Username',
            'email' => 'Email',
            'password' => 'Password',
            'block' => 'Block',
            'send_email' => 'Send Email',
            'register_date' => 'Register Date',
            'lastvisit_date' => 'Lastvisit Date',
            'activation' => 'Activation',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('admin_id', $this->admin_id, true);
        $criteria->compare('admin_name', $this->admin_name, true);
        $criteria->compare('username', $this->username, true);
        $criteria->compare('email', $this->email, true);
        $criteria->compare('password', $this->password, true);
        $criteria->compare('block', $this->block);
        $criteria->compare('send_email', $this->send_email);
        $criteria->compare('register_date', $this->register_date, true);
        $criteria->compare('lastvisit_date', $this->lastvisit_date, true);
        $criteria->compare('activation', $this->activation, true);

        return new CActiveDataProvider($this, array(
                    'criteria' => $criteria,
                ));
    }

    /**
     * Checks if the given password is correct.
     * @param string the password to be validated
     * @return boolean whether the password is valid
     */
    public function validatePassword($password) {
        return $this->hashPassword($password, $this->username) == $this->password;
    }

    /**
     * Generates the password hash.
     * @param string password
     * @param string salt
     * @return string hash
     */
    public function hashPassword($password, $salt) {
        return md5($salt . $password);
    }

    /**
	 * Generates a acvivation code that can be used to forgot password function.
	 * @return string the acvivation code
	 */
	protected function generateAcvivationCode()
	{
		return base64_encode(uniqid('',true));
	}

    /**
     * Set some attributes automatically before inserting new record
     * @return boolean
     */
    public function beforeSave() {
        if ($this->isNewRecord) {
            $this->register_date = new CDbExpression('now()');
            $this->activation = $this->generateAcvivationCode();
        }

        return parent::beforeSave();
    }

}